<?php

namespace app\serverfavorite\service\favorite;
use app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-06-12 11:10:09
 * @Description: 收藏分类 app_key为必传字段
 */

class FavoriteCategory extends ApiBase
{
	
	protected $favoriteCategoryLogic = '\app\serverfavorite\logic\FavoriteCategory';
    protected $favoriteCategoryValidate = '\app\serverfavorite\validate\FavoriteCategory';
    protected $addFields = ['pid'=>0,'name'=>' ','profile_pic'=>'','order'=>100,'description'=>'','app_id'=>0,'other_app_read'=>'','is_public'=>0,'action'=>' '];
    protected $editFields = ['id'=>0,'name'=>' ','profile_pic'=>'','order'=>100,'description'=>'','app_id'=>0,'other_app_read'=>'','is_public'=>0,'action'=>' '];

    use \app\servergoods\traits\BaseCheckCate;

    /*
    	参数: $data['id'] $data['app_key']
    	作用：除了添加方法 操作单个或多个分类时，检验传入数据 检验app_key 检验分类存在性等
    */
    protected function _batchCheckCate($data, $scene = '', $operate = ''): array 
    {

    	$validate = validate($this->favoriteCategoryValidate);
        if(!$validate->scene($scene)->check($data)){
            return verify_err(null, $validate->getError());
        }

        if(isset($data['id'])){

	        is_numeric($data['id']) && $data['id'] = [$data['id']];
	        $map['id'] = ['in',$data['id']];
	    	$cateList = $this->logic($this->favoriteCategoryLogic)->dbCateList($map);

	    	if(!$cateList){
	    		return verify_err(null, '所选择的分类不存在');
	    	}
	    	if(count($cateList) != count($data['id'])){
	    		return verify_err(null, '所选择的分类个数与所查询到的分类个数不统一');
	    	}
		}
    	return auto_success($data, '可操作该分类');
    }

    /*添加一个收藏分类*/
    public function addCate(): array 
    {
    	$data = $this->haveIssetReceiveData($this->addFields);
    	$checkData = $this->_baseCheckCate($data, '', 'edit', $this->favoriteCategoryValidate, $this->favoriteCategoryLogic);
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }
        $data['level'] = $data['pid'] ? $checkData['res']['level'] + 1 : 0;
        return $this->logic($this->favoriteCategoryLogic)->addCate($data);
    }

    /*编辑一个收藏分类*/
    public function editCate(): array 
    {
    	$data = $this->issetReceiveData($this->editFields);
    	$checkData = $this->_baseCheckCate($data, '', 'edit', $this->favoriteCategoryValidate, $this->favoriteCategoryLogic);
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }
        return $this->logic($this->favoriteCategoryLogic)->editCate($data);
    }

    /*查看该分类详情*/
    public function oneCateInfo(): array 
    {
    	$data = $this->issetReceiveData(['id'=>0,'app_id'=>0,'action'=>' ']);
        if(!isset($data['id']) && !isset($data['action'])){
            return verify_err(null, 'id和action必须传一个');
        }

    	$checkCate = $this->_batchCheckCate($data);
    	if($checkCate['status'] != SUCCESS_STATUS){
    		return $checkCate;
    	}

    	return $this->logic($this->favoriteCategoryLogic)->oneCateInfo($data);
    }

    /*查看收藏列表*/
    public function cateList(): array 
    {
    	
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data,'cateList','canNoId');
    }

    /*删除某个收藏分类*/
    public function delOneCate(): array 
    {
    	$data = $this->receiveData(['id'=>0,'app_id'=>0]);
    	return $this->_batchDelCate($data, false);
    }

    /*批量删除收藏分类*/
    public function batchDelCate(): array 
    {
    	$data = $this->receiveData(['id'=>[],'app_id'=>0]);
    	return $this->_batchDelCate($data, true);
    }

    /*批量删除收藏分类*/
    protected function _batchDelCate($data, bool $isBatch = false): array 
    {
    	$scene = $isBatch ? 'checkIds' : 'checkId';

    	$checkCate = $this->_batchCheckCate($data, $scene);
    	if($checkCate['status'] != SUCCESS_STATUS){
    		return $checkCate;
    	}

    	return $this->logic($this->favoriteCategoryLogic)->batchDelCate($data);

    }

    /*启用/禁用 一个 分类*/
    public function changeStatus(): array 
    {
    	$data = $this->receiveData(['id'=>0,'app_id'=>0,'status'=>' ']);
    	return $this->_batchChangeSta($data, false);
    }

    /*启用/禁用 多个 分类*/
    public function batchChangeStatus(): array 
    {
    	$data = $this->receiveData(['id'=>[],'app_id'=>0,'status'=>' ']);
    	return $this->_batchChangeSta($data, true);
    }

    /*启用/禁用 一个/多个 分类*/
    protected function _batchChangeSta($data, bool $isBatch = false): array 
    {
    	$scene = $isBatch ? 'batchChangeStatus' : 'changeStatus';

    	$checkCate = $this->_batchCheckCate($data, $scene);
    	if($checkCate['status'] != SUCCESS_STATUS){
    		return $checkCate;
    	}

    	return $this->logic($this->favoriteCategoryLogic)->batchChangeStatus($data);
    }



    

}






